package com.zhuoshi.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import com.zhuoshi.entity.Complex;
import com.zhuoshi.entity.ComplexCustom;
import com.zhuoshi.entity.Project;
import com.zhuoshi.entity.Unit;
import com.zhuoshi.entity.UnitCustom;
import com.zhuoshi.service.ComplexService;
import com.zhuoshi.service.ProjectService;

@Controller
@RequestMapping("complex")
public class ComplexController {
	
	@Autowired
	private ComplexService complexService;
	
	@Autowired
	private ProjectService projectService;
	
	@RequestMapping("selectComplexByProjectIdLiu")
	public String selectComplexByProjectIdLiu(HttpServletResponse response, Integer projectid){
		List<Complex> complexs=complexService.selectComplexByProjectIdLiu(projectid);
		JSONObject json=new JSONObject();
		json.put("complexs", complexs);
		PrintWriter out=null;
		try {
			out=response.getWriter();
			out.print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("selectAllComplexLiu")
	public ModelAndView selectAllComplexLiu(ComplexCustom complex) {
		System.out.println(complex.getCnumber());
		List<ComplexCustom> complexs=complexService.selectAllComplexCustomLiu(complex.getCnumber());
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("complexs", complexs);
		modelAndView.setViewName("/Admin/complexManager.jsp");
			return modelAndView;
	}
	
	@RequestMapping("deleteComplexById")
	public String deleteUnitById(String id) {
		String []ids=id.split(",");
		int a=0;
		for(int i=0;i<ids.length;i++) {
			int rows=complexService.deleteComplexById(Integer.parseInt(ids[i]));
			a+=rows;
		}		
		if(a==ids.length) {
			return "redirect:selectAllComplexLiu.action";
		}else {
			return "redirect:error.jsp";
		}
	}
	
		@RequestMapping("addComplexMessageLiu")
		public ModelAndView addComplexMessageLiu() throws Exception {
			List<Project> projects=projectService.selectAllProjectLiu();
			ModelAndView modelAndView=new ModelAndView();
			modelAndView.addObject("projects", projects);
			modelAndView.setViewName("/Admin/addComplex.jsp");
			return modelAndView;
		}
		
		@RequestMapping("addComplexConfirmLiu")
		public String addComplexConfirmLiu(Complex complex) {
			int rows=complexService.addComplexConfirmLiu(complex);
			if(rows>0) {
				return "redirect:selectAllComplexLiu.action";
			}else {
				return "redirect:error.jsp";
			}
		}
		
		@RequestMapping("updateComplexMessageLiu")
		public ModelAndView updateComplexMessageLiu(int id) throws Exception {
			ComplexCustom complex=complexService.selectComplexCustomById(id);
			List<Project> projects=projectService.selectAllProjectLiu();
			ModelAndView modelAndView=new ModelAndView();
			modelAndView.addObject("projects", projects);
			modelAndView.addObject("complex", complex);
			modelAndView.setViewName("/Admin/updateComplex.jsp");
			return modelAndView;
		}
		
		@RequestMapping("updateComplexConfirmLiu")
		public String updateComplexConfirmLiu(Complex complex) {
			int rows=complexService.updateComplexConfirmLiu(complex);
			if(rows>0) {
				return "redirect:selectAllUnitLiu.action";
			}else {
				return "redirect:error.jsp";
			}
		}
}
